<template>
  <div class="login">
    <el-image class="login-conter"
      src="https://api.alaboshu.com/wwwroot/uploads/api/891-951-503/2023-09-13/650181a1df3d0c967d60ba47.png"></el-image>
    <div class="login-form">
      <h1 class="login-th1">hello !</h1>
      <h3 class="title">阿拉博数后台管理系统</h3>
      <div class="login-tab">
        <div class="login-tab_txt" :class="{'login-tab_active': loginMethod == 1}" @click="loginMethod = 1">密码登录</div>
        <div class="login-tab_txt" :class="{'login-tab_active': loginMethod == 2}" @click="loginMethod = 2">手机号登录</div>
      </div>
      <template v-if="loginMethod == 1">
        <el-form ref="loginRef" :model="loginForm" :rules="loginRules">        
          <el-form-item prop="key">
            <el-input v-model="loginForm.key" type="text" size="large" auto-complete="off" placeholder="账号">
              <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" type="password" size="large" auto-complete="off" placeholder="密码"
              @keyup.enter="handleLogin">
              <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
            </el-input>
          </el-form-item>     
          <el-checkbox class="login-code_check" v-model="loginForm.rememberMe"
              style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
          <el-form-item style="width:100%;">
            <el-button :loading="loading" size="large" type="primary" style="width:100%;" @click.prevent="handleLogin">
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
            <div style="float: right;" v-if="register">
              <router-link class="link-type" :to="'/register'">立即注册</router-link>
            </div>
          </el-form-item>
        </el-form>
      </template>
      <template v-if="loginMethod == 2">
        <el-form ref="loginRef" :model="noteForm" :rules="noteRules">          
          <el-form-item prop="phonenumber">
            <el-input v-model="noteForm.phonenumber" type="text" size="large" auto-complete="off" placeholder="手机号">
              <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
            </el-input>
          </el-form-item>
          <el-form-item prop="smsCode">
            <el-input v-model="noteForm.smsCode" size="large" auto-complete="off" placeholder="验证码" style="width: 63%"
              @keyup.enter="handleLogin">
              <template #prefix><svg-icon icon-class="validCode" class="el-input__icon input-icon" /></template>
            </el-input>
            <div class="login-code" @click="getCode">{{ codeText }}</div>
          </el-form-item>        
          <el-form-item style="width:100%;">
            <el-button :loading="loading" size="large" type="primary" style="width:100%;" @click.prevent="handleLogin">
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
          </el-form-item>
        </el-form>
      </template>
    </div>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2018-2023 阿拉博数 Li All Rights Reserved.</span>
    </div>
  </div>
</template>

<script setup>
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import useUserStore from '@/store/modules/user'

const userStore = useUserStore()
const router = useRouter();
const { proxy } = getCurrentInstance();

// 1表示账号密码登录  2表示手机号验证码登录
const loginMethod = ref(1)
const codeText = ref("获取验证码")
const countDown = ref(60)
const codeShow = ref(true)

// 账号密码登录
const loginForm = ref({
  key: "admin",
  password: "111111",
  rememberMe: false
});

const loginRules = {
  key: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }]
};


// 手机号验证码登录
const noteForm = ref({
  phonenumber: "",
  smsCode: ""
});

const noteRules = {
  phonenumber: [{ required: true, trigger: "blur", message: "请输入您的手机号" }],
  smsCode: [{ required: true, trigger: "change", message: "请输入验证码" }]
};

const codeUrl = ref("");
const loading = ref(false);
// 验证码开关
const captchaEnabled = ref(true);
// 注册开关
const register = ref(false);
const redirect = ref(undefined);

function handleLogin() {
  proxy.$refs.loginRef.validate(valid => {
    if (valid) {
      loading.value = true;
      // 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
      if(loginMethod.value === 1) {
        if (loginForm.value.rememberMe) {
          Cookies.set("key", loginForm.value.key, { expires: 30 });
          Cookies.set("password", encrypt(loginForm.value.password), { expires: 30 });
          Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 });
        } else {
          // 否则移除
          Cookies.remove("key");
          Cookies.remove("password");
          Cookies.remove("rememberMe");
        }
        // 调用action的登录方法
        userStore.login(loginForm.value).then(async () => {
          router.push({ path: redirect.value || "/" });
        }).catch(() => {
          loading.value = false;
        });
      }else if(loginMethod.value === 2) {
        userStore.noteLogin(noteForm.value).then(()=>{

        }).catch(()=>{
          loading.value = false;
        })
      }
    }
  });
}

function getCode() {
  if(!codeShow.value) {
    return
  }
  codeShow.value = false
  userStore.getCode(noteForm.value.phonenumber).then(res => {
    if(res){
      codeDownInit()
    }
  }).catch(()=>{
    codeShow.value = true
  });
}

function codeDownInit() {
  var timer = setInterval(() => {
    countDown.value--
    if (countDown.value > 0) {
      codeText.value = `${countDown.value}s后重新获取`
    } else {
      clearInterval(timer)
      codeText.value = '获取验证码'
      codeShow.value = true
    }
    Cookies.set("codeDown", codeText.value, { expires: 30 })
  }, 1000)
}

function getCookie() {
  const username = Cookies.get("key");
  const password = Cookies.get("password");
  const rememberMe = Cookies.get("rememberMe");
  loginForm.value = {
    key: username === undefined ? loginForm.value.key : username,
    password: password === undefined ? loginForm.value.password : decrypt(password),
    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  };
}

getCookie();
</script>

<style lang='scss' scoped>
.login {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/background.2f742bae.jpg");
  background-size: cover;
  padding-right: 10%;
  padding-left: 10%;
  box-sizing: border-box;
}

.login-conter {
  width: 28vw;
  height: 28vw;
  object-fit: cover;
}

.login-th1 {
  color: #fff;
  font-size: 50px;
  line-height: 1;
}

.title {
  margin: 0px auto 30px auto;
  text-align: left;
  color: #fff;
}

.login-form {
  border-radius: 12px;
  background-image: linear-gradient(to bottom, #3E44BF, #3257BC);
  width: 400px;
  padding: 10px 25px 80px 25px;

  .el-input {
    height: 40px;

    input {
      height: 40px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0px;
  }
}

.login-code_check {
  color: #999;
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  flex: 1;
  height: 40px;
  line-height: 40px;
  text-align: center;
  float: right;
  margin-left: 15px;
  background-color: #fff;
  border-radius: 3px;
  cursor: pointer;
  color: #409EFF;
  &:hover{
    opacity: 0.9;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 40px;
  padding-left: 12px;
}

.login-tab{
  display: flex;
  flex-direction: row;
  align-items: center;
  margin-bottom: 30px;
}

.login-tab_txt{
  flex: 1;
  height: 40px;
  line-height: 40px;
  text-align: center;
  font-size: 18px;
  color: #fff;  
  cursor: pointer;
  position: relative;
}

.login-tab_active{
  &::after{
    content: '';
    width: 50px;
    height: 2px;
    border-radius: 2px;
    background-color: #fff;
    position: absolute;
    left: 50%;
    bottom: 0;
    transform: translateX(-50%);
  }
}
</style>
